package com.gzdh.tang.modu.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzdh.tang.comm.util.ObjectUtil;
import com.gzdh.tang.modu.entity.Fk;
import com.gzdh.tang.modu.mapper.FkMapper;
import com.gzdh.tang.modu.service.FkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@DS("card")
@Service("fkServiceImpl")
@Slf4j
@Primary
public class FkServiceImpl extends ServiceImpl<FkMapper, Fk> implements FkService {


    @Autowired
    FkMapper fkMapper;


    @Override
    public int getCount(Long id) {
        return fkMapper.getCount(id);
    }

    @Override
    public List<Fk> getFkByPage(int pageNum, int pageSize, Long id) {
        return fkMapper.getFkByPage(((pageNum - 1) * pageSize) + 1,
                ((pageNum - 1) * pageSize) + pageSize, id);
    }

    @Override
    public void spjiesuanByCardSn(String cardsn) {
        try {
            fkMapper.spjiesuan(fkMapper.getCardnoByCardsn(cardsn));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void spjiesuan(String cardNo) {
        try {
            fkMapper.spjiesuan(cardNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getCardNo(String cardSn) {
        return fkMapper.getCardnoByCardsn(cardSn);
    }

    @Override
    public Fk getFkByQr2(String qr) throws RuntimeException {
        Map<String,Object> map = fkMapper.getCardNoByQrCode(qr);
        if(map == null) {
            return null;
        }
        if(ObjectUtil.getBoolean(map.get("isUsed"))) {
            throw new RuntimeException("此码无效");
        }
        String cardNo = ObjectUtil.getStr(map.get("cardNo"));
        Fk fk = this.getOne(new QueryWrapper<Fk>().lambda().eq(Fk::getCardno, cardNo));
        fkMapper.updateQrCode(qr);
        return fk;
    }

    @Override
    public List<String> getTsdc(String posnum) {
        String date = new SimpleDateFormat("HH:mm:ss").format(new Date());

        return fkMapper.getTsdc(posnum, date);
    }
}
